﻿using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.VisualElements;
using LiveChartsCore.SkiaSharpView.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;

namespace TCOChart
{
    public partial class FormPie : UserControl
    {
        public IEnumerable<ISeries> Series { get; set; }
        public IEnumerable<ISeries> Series2 { get; set; }
        public IEnumerable<ISeries> Series3 { get; set; }
        public IEnumerable<ISeries> Series4 { get; set; }
        public LabelVisual Title { get; set; }  // 饼图标题设置

        private static int _index = 0;
        private static string[] _names = ["Maria", "Susan", "Charles", "Fiona", "George"];
        public IEnumerable<ISeries> Series5 { get; set; }
        public IEnumerable<ISeries> Series6 { get; set; }
        public IEnumerable<ISeries> Series7 { get; set; }

        public FormPie()
        {
            InitializeComponent();

            Series = new[] { 2,4,1,4,3}.AsPieSeries();
            Series2 = [
                new PieSeries<int> { Values = [2] },
                new PieSeries<int> { Values = [4] },
                new PieSeries<int> { Values = [1] },
                new PieSeries<int> { Values = [4] },
                new PieSeries<int> { Values = [3] },
                new PieSeries<int> { Values = [6] },
                ];
            Series3 = new[] { 6, 5, 4, 3, 2 }.AsPieSeries((value, series) =>
            {
                // 将等于6的饼图推出60像素显示
                if (value == 6) series.Pushout = 60;
                series.MaxRadialColumnWidth = 80;
            });
            Series4 = new[] { 2, 4, 1, 4, 3 }.AsPieSeries((value, series) =>
            {
                series.MaxRadialColumnWidth = 180;  // 通过设置最大宽度可以绘制圆环图
            });
            Series5 = new[] { 8, 6, 5, 3, 3 }.AsPieSeries((value, series) =>
            {
                series.Name = _names[_index++ % _names.Length];  // 设置每一块饼图名称，通过_names索引指定
                series.DataLabelsPosition = LiveChartsCore.Measure.PolarLabelsPosition.Outer;  // 提示信息显示位置 外部 默认是内部。
                series.DataLabelsSize = 15;  // 显示内容大小设置
                series.DataLabelsPaint = new SolidColorPaint(new SKColor(30, 30, 30));  // 设置颜色
                series.DataLabelsFormatter =
                   point => $"{point.Coordinate.PrimaryValue} " + $"/ {point.StackedValue!.Total}";  // 提示内容显示 如8/25
                series.ToolTipLabelFormatter = point => $"{point.StackedValue!.Share:P2}";  // 触碰提示显示 百分比形式 如20%
            });

            var outer = 0;
            var data = new[] { 6, 5, 4, 3 ,7};
            Series6 = data.AsPieSeries((value, series) =>
            {

                series.OuterRadiusOffset = outer;
                outer += 50;  // 设置每一个饼图向外偏移量，依次增加50
                series.InnerRadius = 50;

                series.DataLabelsPaint = new SolidColorPaint(SKColors.White)
                {
                    SKTypeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold)
                };

                series.ToolTipLabelFormatter =
                    point =>
                    {
                        var pv = point.Coordinate.PrimaryValue;  // 获取该块图形所占值
                        var sv = point.StackedValue!;  // 获取饼图的值属性

                        var a = $"{pv}/{sv.Total}{Environment.NewLine}{sv.Share:P2}";  // 两条提示信息，分数和百分数 {Environment.NewLine}换行
                        return a;
                    };

                series.DataLabelsFormatter =
                    point =>
                    {
                        var pv = point.Coordinate.PrimaryValue;
                        var sv = point.StackedValue!;

                        var a = $"{pv}/{sv.Total}{Environment.NewLine}{sv.Share:P2}";
                        return a;
                    };
            });
            Series7 = GaugeGenerator.BuildSolidGauge(
                new GaugeItem(
                    100,          // the gauge value
                    series =>    // the series style
                    {
                        series.MaxRadialColumnWidth = 15;
                        series.DataLabelsSize = 0;  // 数据标签设置为0不显示
                        series.Fill = new SolidColorPaint(SKColors.YellowGreen);
                    }),
                new GaugeItem(
                    54.1,          // the gauge value
                    series =>    // the series style
                    {
                        series.MaxRadialColumnWidth = 15;
                        series.DataLabelsPaint = new SolidColorPaint(SKColors.Transparent);  // 设置为透明 不显示
                        series.Fill = new SolidColorPaint(SKColors.Black);
                    }),
                new GaugeItem(
                    40,          // the gauge value
                    series =>    // the series style
                    {
                        series.MaxRadialColumnWidth = 15;
                        series.DataLabelsSize = 15;  // 在圆心中间显示数据
                        series.Fill = new SolidColorPaint(SKColors.Brown);
                        //series.HoverPushout = 2;
                    }),
                new GaugeItem(GaugeItem.Background, series =>
                {
                    //series.InnerRadius = 75;
                    series.Fill = new SolidColorPaint(new SKColor(100, 181, 246, 90));
                })
            );
            

            Title = new LabelVisual()
            {
                Text = "Series7",
                TextSize = 15,
                Padding = new LiveChartsCore.Drawing.Padding(15)
            };
            var Title6 = new LabelVisual()
            {
                Text = "Series6",
                TextSize = 15,
                Padding = new LiveChartsCore.Drawing.Padding(15)
            };
            var Title5 = new LabelVisual()
            {
                Text = "Series5",
                TextSize = 15,
                Padding = new LiveChartsCore.Drawing.Padding(15)
            };
            var Title4 = new LabelVisual()
            {
                Text = "Series4",
                TextSize = 15,
                Padding = new LiveChartsCore.Drawing.Padding(15)
            };
            var Title3 = new LabelVisual()
            {
                Text = "Series3",
                TextSize = 15,
                Padding = new LiveChartsCore.Drawing.Padding(15)
            };
            var Title2 = new LabelVisual()
            {
                Text = "Series2",
                TextSize = 15,
                Padding = new LiveChartsCore.Drawing.Padding(15)
            };

            pieChart2.Series = Series2;
            pieChart2.Title = Title2;

            pieChart3.Series = Series3;
            pieChart3.Title = Title3;


            pieChart4.Series = Series4;
            pieChart4.Title = Title4;


            pieChart5.Series = Series5;
            pieChart5.Title = Title5;


            pieChart6.Series = Series6;
            pieChart6.Title = Title6;


            pieChart7.Series = Series7;
            pieChart7.Title = Title;
            pieChart7.MinValue = 0;
            pieChart7.MaxValue = 100;
            //pieChart1.InitialRotation = -45;  // 图形旋转角度
            pieChart7.MaxAngle = 270;
        }
    }
}
